/**
 * 服务
 */
define(["core","core.request","angular.util"],function(core,request,util)
{
	var service={};
	
	//用户
	service.user=
	{
		//返回登录令牌
		getToken:function()
		{
			return core.token.get();
		},
		//登录
		login:function(account,password,callback)
		{
			callback=util.defaultCallback(callback);
			request.user.login(account,password,function(result)
			{
				if(result.success)
				{
					core.token.set(result.message);
					service.user.getMessageByToken(null,true);
					result.message=core.message.message.LOGIN_SUCCESS;
				}
				else
					result=core.message.parseResult(result);
				
				callback(result);
			});
		},
		//注册
		register:function(account,password,callback)
		{
			request.user.register(account,password,util.resultCallback(callback));
		},
		//登出
		logout:function(callback)
		{
			request.user.logout(function(result)
			{
				result=core.message.parseResult(result);
				if(result.success)
				{
					service.user.getMessageByToken(null,true);
					core.token.remove();
				}
				
				callback(result);
			});
		},
		//返回信息
		getMessageByToken:function()
		{
			var message={message:null,last_time:0};
			
			return function(callback,update)
			{
				if(core.util.isBlank(update))
					update=false;
				
				callback=util.defaultCallback(callback);
				
				if((new Date().getTime()-message.last_time)>1000*20 || update)
				{
					request.user.getMessageByToken(function(data)
					{
						message.last_time=new Date().getTime();
						message.message=data;
						callback(data);
					});
				}
				else
					callback(message.message);
			};
		}(),
		//根据ID返回信息
		getMessageById:function(id,callback)
		{
			request.user.getMessageById(id,callback);
		},
		//首页信息
		home:function(id,callback)
		{
			request.user.home(id,callback);
		},
		//编辑
		edit:function(user,callback)
		{
			request.user.edit(user.headphoto,user.username,user.signature,util.resultCallback(callback));
		},
		//修改密码
		updatePassword:function(user,callback)
		{
			request.user.updatePassword(user.oldPassword,user.newPassword,util.resultCallback(callback));
		},
		//注册认证
		registerCheck:function(identity,callback)
		{
			request.user.registerCheck(identity,util.resultCallback(callback));
		},
		bar:
		{
			//返回
			find:function(callback)
			{
				request.user.bar.find(function(data)
				{
					if(core.util.isBlank(data))
						data=new Array();
					
					var root=[];
					for(var i=0,len=data.length;i<len;i++)
					{
						var temp=data[i];
						if(temp.parent==0)
						{
							temp.children=[];
							root.push(temp);
						}
					}
					
					for(var i=0,len=root.length;i<len;i++)
					{
						for(var j=0;j<data.length;j++)
						{
							var temp=data[j];
							if(temp.parent==root[i].id)
								root[i].children.push(temp);
						}
					}
					data=root;
					callback(data);
				});
			}
		},
		menu:
		{
			//返回
			find:function(callback)
			{
				request.user.menu.find(function(data)
				{
					if(core.util.isBlank(data))
						data=new Array();
					
					var root=[];
					for(var i=0,len=data.length;i<len;i++)
					{
						var temp=data[i];
						if(temp.parent==0)
						{
							temp.children=[];
							root.push(temp);
						}
					}
					
					for(var i=0,len=root.length;i<len;i++)
					{
						for(var j=0;j<data.length;j++)
						{
							var temp=data[j];
							if(temp.parent==root[i].id)
								root[i].children.push(temp);
						}
					}
					
					data=root;
					callback(data);
				});
			}
		},
		follow:
		{
			//关注
			create:function(target,callback)
			{
				request.user.follow.create(target,util.resultCallback(callback));
			},
			//取消关注
			clear:function(target,callback)
			{
				request.user.follow.clear(target,util.resultCallback(callback));
			}
		},
		active:
		{
			//返回用户动态
			findByUser:function(id,pn,callback)
			{
				request.user.active.findByUser(id,pn,util.pageCallback(callback));
			}
		},
		mail:
		{
			//根据类型返回
			findByType:function(type,pn,callback)
			{
				request.user.mail.findByType(type,pn,util.pageCallback(callback));
			},
			//返回未读
			getUnread:function(callback)
			{
				request.user.mail.getUnread(util.dataCallback(callback));
			},
			//创建
			create:function(mail,callback)
			{
				request.user.mail.create(mail.title,mail.content,mail.target,util.resultCallback(callback));
			},
			//设置已读
			setRead:function(id,callback)
			{
				request.user.mail.setRead(id,util.resultCallback(callback));
			},
			//删除
			remove:function(id,callback)
			{
				request.user.mail.remove(id,util.resultCallback(callback));
			},
			type:
			{
				//返回类型
				find:function(callback)
				{
					request.user.mail.type.find(util.dataCallback(callback));
				}
			}
		}
	};
	
	//动弹
	service.dongtan=
	{
		//根据ID返回
		findById:function(id,callback)
		{
			request.dongtan.findById(id,callback);
		},
		//最新动弹
		upToDate:function(callback,pn)
		{
			if(core.util.isBlank(pn))
				pn=null;
			
			request.dongtan.upToDate(pn,callback);
		},
		//最热动弹
		hot:function(callback,pn)
		{
			if(core.util.isBlank(pn))
				pn=null;
			
			request.dongtan.hot(pn,util.dataCallback(callback));
		},
		//发表动弹
		create:function(dongtan,callback)
		{
			request.dongtan.create(dongtan.content,util.resultCallback(callback));
		},
		//编辑
		edit:function(dongtan,callback)
		{
			request.dongtan.edit(dongtan.id,dongtan.content,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			request.dongtan.remove(id,util.resultCallback(callback));
		},
		comment:
		{
			//发表评论
			create:function(comment,callback)
			{
				request.dongtan.comment.create(comment.id,comment.content,comment.quote,util.resultCallback(callback));
			}
		}
	};
	
	//评论
	service.comment=
	{
		//编辑
		edit:function(comment,callback)
		{
			request.comment.edit(comment.id,comment.content,util.resultCallback(callback));
		},
		//根据ID返回
		findByCommentId:function(id,pn,callback)
		{
			request.comment.findByCommentId(id,pn,util.pageCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			request.comment.remove(id,util.resultCallback(callback));
		},
		//返回引用
		findByQuote:function(id,pn,callback)
		{
			request.comment.findByQuote(id,pn,util.pageCallback(callback));
		},
		report:
		{
			//举报
			create:function(report,callback)
			{
				request.comment.report.create(report.id,report.content,util.resultCallback(function(result)
				{
					if(result.success)
						result.message=core.message.message.REPORT_CREATE_SUCCESS;
						
					callback(result);
				}));
			}
		}
	};
	
	service.openApi=
	{
		//返回
		find:function(callback)
		{
			request.openApi.find(function(data)
			{
				if(core.util.isBlank(data))
					data=new Array();
				
				var root=[];
				for(var i=0,len=data.length;i<len;i++)
				{
					var temp=data[i];
					if(temp.parent==0)
					{
						temp.children=[];
						root.push(temp);
					}
				}
				
				for(var i=0;i<data.length;i++)
				{
					var temp=data[i];
					if(temp.parent!=0)
					{
						for(var j=0;j<root.length;j++)
						{
							if(temp.parent==root[j].id)
							{
								root[j].children.push(temp);
								break;
							}
						}
					}
				}
				data=root;
				callback(data);
			});
		}
	};
	
	//软件
	service.software=
	{
		//搜索
		search:function(keyword,callback)
		{
			request.software.search(keyword,util.pageCallback(callback));
		},
		//根据ID返回
		findById:function(id,callback)
		{
			request.software.findById(id,callback);
		},
		//返回首页推荐
		findByRecommendIndex:function(callback)
		{
			request.software.findByRecommendIndex(util.dataCallback(callback));
		},
		//返回推荐
		findByRecommend:function(pn,callback)
		{
			request.software.findByRecommend(pn,util.pageCallback(callback));
		},
		//创建
		create:function(software,callback)
		{
			callback=util.defaultCallback(callback);
			request.software.create(software.name,software.home_page,software.doc_url,software.download_url,software.introduce,function(result)
			{
				result=core.message.parseResult(result);
				if(result.success)
					result.message=core.message.message.SOFTWARE_CREATE_SUCCESS;
				
				callback(result);
			});
		},
		//设置推荐
		setRecommend:function(id,callback)
		{
			request.software.setRecommend(id,util.resultCallback(callback));
		},
		//编辑
		edit:function(software,callback)
		{
			request.software.edit(software.id,software.name,software.home_page,software.doc_url,software.download_url,software.introduce,util.resultCallback(callback));
		},
		favorite:
		{
			//关注
			create:function(id,callback)
			{
				request.software.favorite.create(id,util.resultCallback(function(result)
				{
					if(result.success)
						result.message=core.message.message.FAVORITE_CREATE_SUCCESS;
						
					callback(result);
				}));
			},
			//删除关注
			remove:function(id,callback)
			{
				request.software.favorite.remove(id,util.resultCallback(callback));
			}
		}
	};
	
	//讨论
	service.discuss=
	{
		//搜索
		search:function(keyword,callback)
		{
			request.discuss.search(keyword,util.pageCallback(callback));
		},
		//根据软件返回
		findBySoftware:function(id,callback)
		{
			request.discuss.findBySoftware(id,util.pageCallback(callback));
		},
		//首页
		index:function(callback)
		{
			request.discuss.index(callback);
		},
		//根据ID返回
		findById:function(id,callback)
		{
			request.discuss.findById(id,callback);
		},
		//根据板块返回
		findByForum:function(forum,pn,callback)
		{
			request.discuss.findByForum(forum,pn,util.pageCallback(callback));
		},
		//编辑
		edit:function(discuss,callback)
		{
			request.discuss.edit(discuss.id,discuss.title,discuss.content,util.resultCallback(callback));
		},
		//移动板块
		moveForum:function(id,forumId,callback)
		{
			request.discuss.moveForum(id,forumId,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			request.discuss.remove(id,util.resultCallback(callback));
		},
		comment:
		{
			//创建评论
			create:function(comment,callback)
			{
				request.discuss.comment.create(comment.id,comment.content,comment.quote,util.resultCallback(callback));
			}
		},
		//创建
		create:function(discuss,callback)
		{
			callback=util.defaultCallback(callback);
			
			discuss.tags=[discuss.tag_1,discuss.tag_2,discuss.tag_3,discuss.tag_4,discuss.tag_5];
			
			request.discuss.create(discuss.title,discuss.content,discuss.forum,discuss.tags,function(result)
			{
				result=core.message.parseResult(result);
				if(result.success)
					result.message=core.message.message.DISCUSS_CREATE_SUCCESS;
				
				callback(result);
			});
		},
		//更新排序
		updateOrder:function(id,order,callback)
		{
			request.discuss.updateOrder(id,order,util.resultCallback(callback));
		},
		//板块
		forum:
		{
			//返回
			find:function(callback)
			{
				request.discuss.forum.find(util.dataCallback(callback));
			},
			//返回标签
			findTag:function(forum,callback)
			{
				request.discuss.forum.findTag(forum,util.dataCallback(callback));
			}
		},
		order:
		{
			//返回
			find:function(callback)
			{
				request.discuss.order.find(util.dataCallback(callback));
			}
		},
		favorite:
		{
			//关注
			create:function(id,callback)
			{
				request.discuss.favorite.create(id,util.resultCallback(function(result)
				{
					if(result.success)
						result.message=core.message.message.FAVORITE_CREATE_SUCCESS;
						
					callback(result);
				}));
			},
			//删除关注
			remove:function(id,callback)
			{
				request.discuss.favorite.remove(id,util.resultCallback(callback));
			}
		},
		report:
		{
			//举报
			create:function(report,callback)
			{
				request.discuss.report.create(report.id,report.content,util.resultCallback(function(result)
				{
					if(result.success)
						result.message=core.message.message.REPORT_CREATE_SUCCESS;
						
					callback(result);
				}));
			}
		}
	};
	
	service.friendlink=
	{
		//返回
		find:function(callback)
		{
			request.friendlink.find(util.dataCallback(callback));
		}
	};
	
	service.blog=
	{
		//搜索
		search:function(keyword,callback)
		{
			request.blog.search(keyword,util.pageCallback(callback));
		},
		//根据作者返回最新
		findNewByAuthor:function(id,callback)
		{
			request.blog.findNewByAuthor(id,util.dataCallback(callback));
		},
		//根据作者返回
		findByAuthor:function(id,pn,callback)
		{
			request.blog.findByAuthor(id,pn,util.pageCallback(callback));
		},
		//根据作者和目录返回
		findByAuthorAndCatalog:function(id,catalog,pn,callback)
		{
			request.blog.findByAuthorAndCatalog(id,catalog,pn,util.pageCallback(callback));
		},
		//根据ID返回
		findById:function(id,callback)
		{
			request.blog.findById(id,callback);
		},
		//返回最新推荐
		findRecommend:function(pn,callback)
		{
			request.blog.findRecommend(pn,util.pageCallback(callback));
		},
		//创建
		create:function(blog,callback)
		{
			request.blog.create(blog.title,blog.content,blog.order,blog.catalog,blog.comment,blog.oneself,util.resultCallback(callback));
		},
		//编辑
		edit:function(blog,callback)
		{
			request.blog.edit(blog.id,blog.title,blog.content,blog.order.id,blog.catalog.id,blog.comment,blog.oneself,util.resultCallback(callback));
		},
		//删除
		remove:function(id,callback)
		{
			request.blog.remove(id,util.resultCallback(callback));
		},
		//设置/取消推荐
		setRecommend:function(id,callback)
		{
			request.blog.setRecommend(id,util.resultCallback(callback));
		},
		comment:
		{
			//创建评论
			create:function(comment,callback)
			{
				request.blog.comment.create(comment.id,comment.content,comment.quote,util.resultCallback(callback));
			}
		},
		order:
		{
			//返回排序
			find:function(callback)
			{
				request.blog.order.find(util.dataCallback(callback));
			}
		},
		catalog:
		{
			//返回当前用户的目录
			find:function(callback)
			{
				request.blog.catalog.find(util.dataCallback(callback));
			},
			//创建
			create:function(catalog,callback)
			{
				request.blog.catalog.create(catalog.name,util.resultCallback(callback));
			},
			//更新
			update:function(catalog,callback)
			{
				request.blog.catalog.update(catalog.id,catalog.name,util.resultCallback(callback));
			},
			//删除
			remove:function(id,target,callback)
			{
				request.blog.catalog.remove(id,target,util.resultCallback(callback));
			},
			//根据用户返回
			findByUser:function(id,callback)
			{
				request.blog.catalog.findByUser(id,util.dataCallback(callback));
			}
		},
		favorite:
		{
			//关注
			create:function(id,callback)
			{
				request.blog.favorite.create(id,util.resultCallback(function(result)
				{
					if(result.success)
						result.message=core.message.message.FAVORITE_CREATE_SUCCESS;
						
					callback(result);
				}));
			},
			//删除关注
			remove:function(id,callback)
			{
				request.blog.favorite.remove(id,util.resultCallback(callback));
			}
		},
		recommend:
		{
			//自荐
			create:function(id,callback)
			{
				request.blog.recommend.create(id,util.resultCallback(callback));
			}
		}
	};
	
	service.favorite=
	{
		//根据类型返回
		findByType:function(type,pn,callback)
		{
			request.favorite.findByType(type,pn,util.pageCallback(callback));
		},
		type:
		{
			//返回
			find:function(callback)
			{
				request.favorite.type.find(util.dataCallback(callback));
			}
		}
	};
	
	return service;
});